package br.com.news.activity;

import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;
import br.com.news.R;
import br.com.news.exception.ServiceException;
import br.com.news.modal.CategoriaBean;
import br.com.news.service.CategoriaService;
import br.com.news.service.interfaces.ICategoriaService;
import br.com.news.util.Constantes;

/**
 * Activity para selecionar categorias desejadas.
 * 
 * @author Jefferson
 * @since 19/03/2013
 */
public class CategoriaActivity extends GenericActivity implements Runnable{

	private TextView txtAtualizarCategoria;
	private ListView listCategoria;
	private ProgressDialog dialog;
	
	private Handler handler;
	
	private List<CategoriaBean> categoriaBeans;
	
	private ICategoriaService categoriaService;
	
	@Override
	protected int getContentView() {
		return R.layout.activity_categoria;
	}
	
	@Override
	protected void preConstruct() throws Exception {
		handler = new Handler();
		categoriaService = new CategoriaService(this);
	}
	
	@Override
	protected void recuperaElementosDoXml() throws Exception {
		txtAtualizarCategoria = (TextView) findViewById(R.activity_categoria.txtAtualizarCategoria);
		listCategoria = (ListView) findViewById(android.R.id.list);
	}
	
	@Override
	protected void instanciaElementosDoXml() throws Exception {
		carregaListaCategoria();
	}
	
	@Override
	protected void defineAcaoDosElementos() throws Exception {
		txtAtualizarCategoria.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				sincronizarCategoria();
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(categoriaService != null){
			categoriaService.fechar();
		}
	}
	
	public void sincronizarCategoria(){
		dialog = ProgressDialog.show(this, getString(R.string.sindronizando), getString(R.string.progress_categoria));
		new Thread(this).start();
	}

	@Override
	public void run() {
		try {
			categoriaService.sincronizar();
			handler.post(new Runnable() {
				@Override
				public void run() {
					carregaListaCategoria();
					dialog.dismiss();
				}
			});
		} catch (ServiceException e) {
			Log.e(Constantes.LOG, e.getMessage(), e);
			handler.post(new Runnable() {
				@Override
				public void run() {
					dialog.dismiss();
					ErroActivity.chamaActivityErro(CategoriaActivity.this, R.string.falha_sincronizar);
					CategoriaActivity.this.finish();
				}
			});
		}
	}
	
	private void carregaListaCategoria(){
		try {
			categoriaBeans = categoriaService.findAll();
			listCategoria.setAdapter(new CategoriaAdapter(this, categoriaBeans, categoriaService));
		} catch (ServiceException e) {
			Log.e(Constantes.LOG, e.getMessage(), e);
			ErroActivity.chamaActivityErro(this, R.string.falha_buscar_categorias);
		}
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
		finish();
	}
	
}
